﻿
using Dapper;
using Enterprise_Get.API.Infrastructures.Interfaces;
using MySql.Data.MySqlClient;
using SlaughterItem.Common.DBContext;
using System.Data;

namespace Enterprise_Get.API.Infrastructures.Repositories
{
    /// <summary>
    /// 基类仓储
    /// </summary>
    /// <typeparam name="T">占位符</typeparam>
    public class BaseRepositoryGet<T> : IBaseRepositoryGet<T> where T : class, new()
    {
        private readonly IDbConnection Db;
        private readonly IConfiguration _configuration;

        public BaseRepositoryGet(IConfiguration configuration)
        {
            _configuration = configuration;
            Db = new MySqlConnection(_configuration.GetConnectionString("Whq"));
        }

        /// <summary>
        /// 获取所有数据
        /// </summary>
        /// <returns>所有数据</returns>
        public IEnumerable<T> GetAll(string sql)
        {
            try
            {
                var list = Db.Query<T>(sql);
                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public int Add(string sql, object param)
        {
            try
            {
                var res = Db.Execute(sql, param);
                return res;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public int Update(string sql, object Data)
        {
            try
            {
                var res = Db.Execute(sql, Data);
                return res;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public int Delete(string sql, object Data)
        {
            try
            {
                var res = Db.Execute(sql, Data);
                return res;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
